<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class EmailExport implements FromCollection, WithColumnWidths, WithEvents, WithHeadings
{
    protected $data;

    public function __construct(Collection $data)
    {
        $this->data = $data;
    }

    public function collection()
    {
        return $this->data;
        $emptyRows = collect([['', '', ''], ['', '', ''], ['', '', '']]); // 生成3行空行
        $data = $this->data;

        // 设置标题行
        $header = collect([[
            '提单号',
            '分单号',
            '目的地',
            '件数',
            '重量(kgs)',
            '方数(m³)',
            '交货日期',
            '交货车牌',
            '包装 件数',
            '入仓单号',
            '是否按电交',
            '备注',
        ]]);

        // 合并空行、标题行和数据
        return $emptyRows->concat($header)->concat($data);
    }

    public function columnWidths(): array
    {
        // 设置每列的宽度
        return [
            'A' => 17,
            'B' => 17,
            'C' => 17,
            'D' => 17,
            'E' => 14,
            'F' => 14,
            'G' => 14,
            'H' => 14,
            'I' => 23,
            'L' => 23,
        ];
    }

    public function headings(): array {
        return [
            '提单号',
            '分单号',
            '目的地',
            '件数',
            '重量(kgs)',
            '方数(m³)',
            '交货日期',
            '交货车牌',
            '包装 件数',
            '入仓单号',
            '是否按电交',
            '备注',
        ];
    }


    public function registerEvents(): array
    {
        return [
            BeforeSheet::class => function(BeforeSheet $event) {
                // 合并单元格
                $event->sheet->getDelegate()->mergeCells('A1:L1'); // 合并第一行 A 到 L
                $event->sheet->getDelegate()->setCellValue('A1', 'EVERY MOMENT LOGISTICS CO.,LTD');

                $event->sheet->getDelegate()->getStyle('A1:L1')->getFill()->setFillType(Fill::FILL_SOLID);
                $event->sheet->getDelegate()->getStyle('A1:L1')->getFill()->getStartColor()->setARGB('FFDAE1F0');

                $event->sheet->getDelegate()->mergeCells('A2:D2'); // 合并第二行 A 到 D
                $event->sheet->getDelegate()->setCellValue('A2', '目的地');

                $event->sheet->getDelegate()->mergeCells('E2:F2'); // 合并第二行 E 到 F
                $event->sheet->getDelegate()->setCellValue('E2', '航班信息');

                $event->sheet->getDelegate()->mergeCells('G2:H2'); // 合并第二行 G 到 H
                $event->sheet->getDelegate()->setCellValue('G2', '起飞时间');

                $event->sheet->getDelegate()->setCellValue('I2', '板型');

                $event->sheet->getDelegate()->mergeCells('J2:K2'); // 合并第二行 J 到 K
                $event->sheet->getDelegate()->setCellValue('J2', 'PIVOT');

                $event->sheet->getDelegate()->setCellValue('L2', '交货时间');

                // 保持第三行与第二行一致
                $data = ($this->data->toArray());
                $event->sheet->getDelegate()->mergeCells('A3:D3'); // 合并第三行 A 到 D
                $event->sheet->getDelegate()->setCellValue('A3', $data[0]['des'] ?? '');

                $event->sheet->getDelegate()->mergeCells('E3:F3'); // 合并第三行 E 到 F
                $event->sheet->getDelegate()->setCellValue('E3', $data[0]['flight_number'] ?? '');

                $event->sheet->getDelegate()->mergeCells('G3:H3'); // 合并第三行 G 到 H
                $event->sheet->getDelegate()->setCellValue('G3', $data[0]['take_off_time'] ?? '');

                $event->sheet->getDelegate()->setCellValue('I3', $data[0]['bans'] ?? '');

                $event->sheet->getDelegate()->mergeCells('J3:K3'); // 合并第三行 J 到 K
                $event->sheet->getDelegate()->setCellValue('J3', $data[0]['pivot'] ?? '');

                $event->sheet->getDelegate()->setCellValue('L3', $data[0]['delivery_time'] ?? '');

                // 设置居中对齐
                $alignment = new Alignment();
                $alignment->setHorizontal(Alignment::HORIZONTAL_CENTER);
                $alignment->setVertical(Alignment::VERTICAL_CENTER);

                // 应用居中对齐到所有合并的单元格
                $event->sheet->getDelegate()->getStyle('A1:L1')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('A1:L1')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('A2:D2')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('A2:D2')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('E2:F2')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('E2:F2')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('G2:H2')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('G2:H2')->getAlignment()->setVertical($alignment->getVertical());

                $event->sheet->getDelegate()->getStyle('I2')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('I2')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('I3')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('I3')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('J2:K2')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('J2:K2')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('L2')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('L2')->getAlignment()->setVertical($alignment->getVertical());

                $event->sheet->getDelegate()->getStyle('L3')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('L3')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('A3:D3')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('A3:D3')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('E3:F3')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('E3:F3')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('G3:H3')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('G3:H3')->getAlignment()->setVertical($alignment->getVertical());
                
                $event->sheet->getDelegate()->getStyle('J3:K3')->getAlignment()->setHorizontal($alignment->getHorizontal());
                $event->sheet->getDelegate()->getStyle('J3:K3')->getAlignment()->setVertical($alignment->getVertical());

                // // 第四行标题行居中对齐
                // $event->sheet->getDelegate()->getStyle('A4:L4')->getAlignment()->setHorizontal($alignment->getHorizontal());
                // $event->sheet->getDelegate()->getStyle('A4:L4')->getAlignment()->setVertical($alignment->getVertical());

                unset($data[0]);
                $this->data = collect(array_values($data));

                $sheet = $event->sheet->getDelegate();
                $sheet->getRowDimension(1)->setRowHeight(25);
                $sheet->getRowDimension(2)->setRowHeight(25);
                $sheet->getRowDimension(3)->setRowHeight(80);
            },
        ];
    }

}